package com.huawei.hms.framework.network.restclient.hwhttp.cronet;

import android.content.Context;
import android.text.TextUtils;
import com.huawei.hms.framework.common.CreateFileUtil;
import com.huawei.hms.framework.common.Logger;
import com.huawei.hms.framework.common.StringUtils;
import com.huawei.hms.framework.network.restclient.hwhttp.HttpClient;
import com.huawei.hms.framework.network.restclient.hwhttp.RequestTask;
import com.huawei.hms.framework.network.restclient.hwhttp.RequestTask.Factory;
import java.io.File;
import java.util.Map.Entry;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import org.chromium.net.CronetEngine;
import org.chromium.net.ExperimentalCronetEngine;
import org.chromium.net.ExperimentalCronetEngine.Builder;
import org.json.JSONException;
import org.json.JSONObject;

public class CronetRequestTaskFactory implements Factory {
    private static final String TAG = "CronetRequestTaskFactor";
    private static volatile CronetRequestTaskFactory instance;
    private CronetEngine cronetEngine;
    private CronetEventListener cronetEventListener;
    private HttpClient httpClient;
    private Executor networkQualityExecutor;

    public static CronetRequestTaskFactory getInstance(Context context) {
        if (instance == null) {
            synchronized (CronetRequestTaskFactory.class) {
                if (instance == null) {
                    instance = new CronetRequestTaskFactory(context);
                }
            }
        }
        return instance;
    }

    private void buildCronetEngine(Context context) {
        try {
            Builder enableNetworkQualityEstimator = new Builder(context).enableQuic(true).enableHttp2(true).enableNetworkQualityEstimator(true);
            for (Entry entry : CronetNegotiateManager.getInstance().getQuicHints().entrySet()) {
                Logger.i(TAG, "the host of using quic is %s", entry.getKey());
                enableNetworkQualityEstimator.addQuicHint((String) entry.getKey(), ((QuicHint) entry.getValue()).getPort(), ((QuicHint) entry.getValue()).getAlternatePort());
            }
            try {
                enableNetworkQualityEstimator.setExperimentalOptions(new JSONObject().put("QUIC", new JSONObject().put("connection_options", "STMP")).toString());
            } catch (JSONException e) {
                Logger.e(TAG, "set QUIC options failed, exception:", e.getClass().getSimpleName());
            }
            synchronized (this) {
                this.cronetEngine = enableNetworkQualityEstimator.build();
                this.networkQualityExecutor = Executors.newSingleThreadExecutor();
                this.cronetEventListener = new CronetEventListener(this.networkQualityExecutor);
                if (this.cronetEngine instanceof ExperimentalCronetEngine) {
                    ((ExperimentalCronetEngine) this.cronetEngine).addRequestFinishedListener(this.cronetEventListener);
                }
            }
        } catch (Exception e2) {
            Logger.i(TAG, "build CronetEngine failed, the reason:" + StringUtils.anonymizeMessage(e2.getMessage()));
        }
    }

    private CronetRequestTaskFactory(Context context) {
        if (context == null) {
            Logger.w(TAG, "the context is null,and the CronetRequestTaskFactory cann't been Initialized!");
        } else {
            buildCronetEngine(context);
        }
    }

    @Deprecated
    public CronetRequestTaskFactory(HttpClient httpClient, Context context) {
        if (context == null) {
            Logger.w(TAG, "the context is null,and the CronetRequestTaskFactory cann't been Initialized!");
            return;
        }
        this.httpClient = httpClient;
        Builder enableNetworkQualityEstimator = new Builder(context).enableQuic(true).enableHttp2(true).enableNetworkQualityEstimator(true);
        if (httpClient.getCache() != null) {
            Object cacheDir = httpClient.getCache().getCacheDir();
            if (!TextUtils.isEmpty(cacheDir)) {
                String str = cacheDir + "/cronet";
                File newFile = CreateFileUtil.newFile(str);
                if (!newFile.exists()) {
                    newFile.mkdirs();
                }
                enableNetworkQualityEstimator.setStoragePath(str);
                enableNetworkQualityEstimator.enableHttpCache(3, httpClient.getCache().getMaxSize());
            }
        }
        for (Entry entry : CronetNegotiateManager.getInstance().getQuicHints().entrySet()) {
            Logger.i(TAG, "the host of using quic is %s", entry.getKey());
            enableNetworkQualityEstimator.addQuicHint((String) entry.getKey(), ((QuicHint) entry.getValue()).getPort(), ((QuicHint) entry.getValue()).getAlternatePort());
        }
        this.cronetEngine = enableNetworkQualityEstimator.build();
        this.networkQualityExecutor = Executors.newSingleThreadExecutor();
        this.cronetEventListener = new CronetEventListener(this.networkQualityExecutor);
        if (this.cronetEngine instanceof ExperimentalCronetEngine) {
            ((ExperimentalCronetEngine) this.cronetEngine).addRequestFinishedListener(this.cronetEventListener);
        }
    }

    public RequestTask newTask() {
        return new CronetRequestTask(this.cronetEngine, this);
    }

    public int getConnectTimeout() {
        return this.httpClient.getConnectTimeout();
    }

    public int getReadTimeout() {
        return this.httpClient.getReadTimeout();
    }

    public CronetEventListener getCronetEventListener() {
        return this.cronetEventListener;
    }
}
